package com.naicheng.ivote.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.naicheng.ivote.R;
import com.naicheng.ivote.doman.HotCommentListBean;
import com.naicheng.ivote.doman.HotCommentsChildBean;
import com.naicheng.ivote.doman.HotOptionsBean;
import com.naicheng.ivote.doman.PublicOptionsBean;
import com.naicheng.ivote.uiself.CommentChildLinearForListView;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PublishOptionsListAdapter extends BaseAdapter {

    private Context context;
	private LayoutInflater mInflater;
    private ArrayList<PublicOptionsBean> list;
    private Handler handler;

    /**
     * 
     * @param context
     * @param list
     */
    public PublishOptionsListAdapter(Context context,ArrayList<PublicOptionsBean> list,Handler handler)
    {
    	this.context=context;
    	this.list = list;
    	this.handler = handler;
    	this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    	final ViewHolder	holder;
    	if(convertView == null){
    		holder = new ViewHolder();
    		convertView = mInflater.inflate(R.layout.public_option_item, null);
    		holder.publish_options_item_tv = (TextView) convertView.findViewById(R.id.publish_options_item_tv);
    		holder.publish_options_item_et = (EditText) convertView.findViewById(R.id.publish_options_item_et);
    		holder.publish_optionsnum_item_tv = (TextView) convertView.findViewById(R.id.publish_optionsnum_item_tv);
    		holder.publish_options_item_del = (TextView) convertView.findViewById(R.id.publish_options_item_del);
    		
    		convertView.setTag(holder);
    		convertView.setId(position);
    	}else{
    		holder = (ViewHolder) convertView.getTag();
    	}
    	
    	if(position>1){
    		holder.publish_options_item_del.setVisibility(View.VISIBLE);
    	}
    	
    	holder.publish_options_item_tv.setText(list.get(position).getSequence()+".");
    	holder.publish_options_item_et.setText(list.get(position).getText());
    	int textnum=holder.publish_options_item_et.getText().toString().length();
    	holder.publish_optionsnum_item_tv.setText(""+(15-textnum));
    	
    	holder.publish_options_item_et.addTextChangedListener(new TextWatcher() {
    		private CharSequence temp;  
            private int selectionStart ;  
            private int selectionEnd ; 
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				holder.publish_optionsnum_item_tv.setText((15-temp.length())+"");
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				temp = s;  
			}
			@Override
			public void afterTextChanged(Editable s) {
				selectionStart = holder.publish_options_item_et.getSelectionStart();  
                selectionEnd = holder.publish_options_item_et.getSelectionEnd();  
                if (temp.length() > 15) {  
                    s.delete(selectionStart-1, selectionEnd);  
                    int tempSelection = selectionStart;  
                    holder.publish_options_item_et.setText(s);  
                    holder.publish_options_item_et.setSelection(tempSelection);  
                }  
                list.get(position).setText(s.toString());
			}
		});
    	holder.publish_options_item_del.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Message msg = new Message();
				Bundle data = new Bundle();
				// 设置选中索引
				data.putInt("position", position);
				msg.setData(data);
				msg.what = 1;
				// 发出消息
				handler.sendMessage(msg);
				
			}
		});
        return convertView;
    }

    public class ViewHolder
    {
    	public TextView publish_options_item_tv;
    	public EditText publish_options_item_et;
    	public TextView publish_optionsnum_item_tv;
    	public TextView publish_options_item_del;
    	
    	
    }
    
}
